home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_12_07
/
allison
/
destroy5.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1994-05-02
|
699b
|
48 lines
LISTING 9 - Illustrates the Principle of "Resource Allocation is
Initialization"
// destroy5.cpp
#include <stdio.h>
void f(char *fname);
main()
{
try
{
f("file1.dat");
}
catch(...)
{
puts("Exception caught in main()");
}
return 0;
}
void f(char *fname)
{
class File
{
FILE *f;
public:
File(const char* fname, const char* mode)
{
f = fopen(fname, mode);
}
~File()
{
fclose(f);
puts("File closed");
}
};
File x(fname,"r");
throw 1;
}
/* Output:
File closed
Exception caught in main()
*/